package com.tech.mapper.tem.imp;

import com.tech.mapper.OrderDao;
import com.tech.repository.entity.Goods;
import com.tech.repository.entity.Order;

import java.sql.*;
import java.util.List;
import java.util.TreeMap;

public class OrderDaoMysqlImp extends BasicDaoImp<Order> implements OrderDao {
	@Override
	public Order getOrder(int id) {
		SqlTask<Integer, Order> sqlTask = (item, data) -> {
			String sql = "select * from orderinfo where boid=?";
			Order order = null;
			PreparedStatement statement = item.prepareStatement(sql);
			statement.setInt(1, data);
			ResultSet rs = item.executeQuery();
			if (rs.next()) {
				order = getInstance(rs);
			}
			return order;
		};
		// 执行sql语句
		return SqlTask.processSql(sqlTask, id);
	}

	@Override
	public Order getOrder(int buyerId, int goodsId) {
		SqlTask<Object, Order> sqlTask = (item, data) -> {
			String sql = "select * from orderinfo where buid = ? and gid = ?";
			Order order = null;
			PreparedStatement statement = item.prepareStatement(sql);
			statement.setInt(1, buyerId);
			statement.setInt(2, goodsId);
			ResultSet rs = item.executeQuery();
			if (rs.next()) {
				order = getInstance(rs);
			}
			return order;
		};
		// 执行sql语句
		return SqlTask.processSql(sqlTask, null);
	}

	@Override
	public List<Order> getUserOrders(int userId) {
		return null;
	}

	@Override
	public TreeMap<Integer, List<Order>> getAllOrders() {
		return null;
	}

	@Override
	public int addOrder(Order order) {
		SqlTask<Order, Integer> sqlTask = (item, data) -> {
			String sql = "insert into orderinfo(buid, gid, adid,bocost,bostate,bosend,bcreatetime) values " +
					"(?,?,?,?,?,?,?)";
			int newId = -1;
			PreparedStatement statement = item.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			statement.setInt(1, data.getBuyerId());
			statement.setInt(2, data.getGoodsId());
			statement.setInt(3, data.getAddressId());
			statement.setDouble(4, data.getCost());
			statement.setString(5, data.getState().toString());
			statement.setString(6, data.getSendMode().toString());
			statement.setDate(7, new Date(data.getCreateTime().getTime()));
			if (statement.executeUpdate() == 1) {
				ResultSet generatedKeys = item.getGeneratedKeys();
				if (generatedKeys.next()) {
					newId = generatedKeys.getInt(1);
				}
			}
			return newId;
		};
		return SqlTask.processSql(sqlTask, order);
	}

	@Override
	public int[] addOrders(List<Order> orderList) {
		return new int[0];
	}

	@Override
	public boolean updateOrderState(int orderId, Order.OrderState orderState) {
		SqlTask<Integer, Boolean> sqlTask = (item, data) -> {
			String sql = "update orderinfo set orderinfo.bostate = ? where boid = ?";
			PreparedStatement statement = item.prepareStatement(sql);
			statement.setString(1, orderState.toString());
			statement.setInt(2, orderId);
			return statement.executeUpdate() > 0;
		};
		return SqlTask.processSql(sqlTask, orderId);
	}

	@Override
	public boolean delOrder(int id) {
		return false;
	}

	@Override
	public int delUserOrder(int userId) {
		return 0;
	}

	@Override
	public int[] delOrders(List<Integer> ordersList) {
		return new int[0];
	}

	@Override
	public boolean delAllOrders() {
		return false;
	}

	@Override
	public boolean updateOrder(int orderId, Order newOrder) {
		SqlTask<Order, Boolean> sqlTask = (item, data) -> {
			String sql = "update orderinfo set bocost  = ?, bostate = ?, bosend  = ? where boid = ?";
			PreparedStatement statement = item.prepareStatement(sql);
			statement.setDouble(1, data.getCost());
			statement.setString(2, data.getState().toString());
			statement.setString(3, data.getSendMode().toString());
			statement.setInt(4, data.getId());
			return statement.executeUpdate() > 0;
		};
		return SqlTask.processSql(sqlTask, newOrder);
	}

	@Override
	public boolean updateOrderState(int orderId, String newState) {
		SqlTask<Object, Boolean> sqlTask = (item, data) -> {
			String sql = "update orderinfo set bostate = ? where boid = ?";
			PreparedStatement statement = item.prepareStatement(sql);
			statement.setString(1, newState);
			statement.setInt(2, orderId);
			return statement.executeUpdate() > 0;
		};
		return SqlTask.processSql(sqlTask, null);
	}

	@Override
	protected Order getInstance(ResultSet rs) throws SQLException {
		return new Order(rs.getInt("boid"),
				rs.getInt("buid"),
				rs.getInt("gid"),
				rs.getInt("adid"),
				rs.getDouble("bocost"),
				Order.OrderState.parse(rs.getString("bostate")),
				Goods.SendMode.parse(rs.getString("bosend")),
				rs.getDate("bcreatetime"));
	}
}
